package com.zr.controller;

import com.github.pagehelper.PageInfo;
import com.zr.pojo.Goods;
import com.zr.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class GoodsController {
    @Resource
    private GoodsService service_goods;

    //首页 根据类型查询 每个类目销量前4名
    @RequestMapping("/selectAll_Goods")
    public String selectAll_Goods(HttpServletRequest request){
        List<Goods> ShuiGuo = service_goods.selectTypeAll("新鲜水果");
        List<Goods> HaiXian = service_goods.selectTypeAll("海鲜水产");
        List<Goods> NiuYang = service_goods.selectTypeAll("猪羊牛肉");
        List<Goods> QinDan = service_goods.selectTypeAll("禽类蛋品");
        List<Goods> ShuCai = service_goods.selectTypeAll("新鲜蔬菜");
        List<Goods> SuDong = service_goods.selectTypeAll("速冻食品");
        request.getSession().setAttribute("ShuiGuo",ShuiGuo);
        request.getSession().setAttribute("HaiXian",HaiXian);
        request.getSession().setAttribute("NiuYang",NiuYang);
        request.getSession().setAttribute("QinDan",QinDan);
        request.getSession().setAttribute("ShuCai",ShuCai);
        request.getSession().setAttribute("SuDong",SuDong);
        return "index";
    }

    //同一类型商品页面
    @RequestMapping("/readmoreShop")
    public String readmoreShop(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                               String type, Model model){
        //根据商品类型+价格
        PageInfo<Goods> page = service_goods.readmoreShop(type,pageNum,10);
        model.addAttribute("goods_Type",page);
        //根据销量推荐
        List<Goods> goods_Num = service_goods.selectNumAll();
        model.addAttribute("goods_Num",goods_Num);
        model.addAttribute("type",type);
        return "readmoreShop";
    }

    //模糊查询
    @RequestMapping("/selectLikeAll")
    public String selectLikeAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, String title,HttpServletRequest request){
        PageInfo<Goods> list = service_goods.selectLike(title,pageNum,10);
        //model.addAttribute("goods_Like",list);
        request.getSession().setAttribute("goods_Like",list);
        List<Goods> goods_Num = service_goods.selectNumAll();
        request.getSession().setAttribute("goods_Num",goods_Num);
        request.getSession().setAttribute("title",title);
        return "search";
    }

    //商品详情页
    @RequestMapping("/goodsInfo")
    public String goodsInfo(@RequestParam Integer gid,HttpServletRequest request){
        //根据接收的商品id查询
        Goods goods_One = service_goods.selectOnt(gid);
        request.getSession().setAttribute("goods_One",goods_One);
        //根据销量推荐
        List<Goods> goods_Num = service_goods.selectNumAll();
        request.getSession().setAttribute("goods_Num",goods_Num);
        return "shop_message";
    }


}
